﻿var xmlHttp;

var visitor = "VeryFTP不能保证资源详细介绍信息的准确性。如果您是VeryFTP<a href='' title='申请成为认证用户'><span class='special'>认证用户</span></a>，发现当前信息有误,可以<a href='javascript:;' onclick='openLoginPanel();' title='登录'><span class='special'>登录</span></a>进行编辑。"

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        }
    }
}

function login(){
	var userName = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	createXMLHttpRequest();
	var url = "login?userName=" + encodeURI(userName)+"&password="+encodeURI(password);
    xmlHttp.onreadystatechange = handleLoginStateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function logout(){
	createXMLHttpRequest();
	var url = "logout";
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	var loginPanel = document.getElementById("loginPanel");
	loginPanel.innerHTML = visitor;
}

function handleLoginStateChanged() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            loginResult();
        }else{
        	alert("status:"+xmlHttp.status);
        }
    }
}

function loginResult(){
	var xmlDoc = xmlHttp.responseXML.documentElement;
	var result = xmlDoc.getElementsByTagName("result").item(0).firstChild.nodeValue;
	
	var loginPanel = document.getElementById("loginPanel");

	if(result == "success"){
		var userName = xmlDoc.getElementsByTagName("userName").item(0).firstChild.nodeValue;
		loginPanel.innerHTML = "当前登录为:"+userName+",点击进行<a href='javascript:;' onclick='modify();' title='编辑'><span class='special'>编辑</span></a>或<a href='javascript:;' onclick='logout();'><span class='special'>退出</span></a>";
	}else if(result == "failure"){
		loginPanel.innerHTML="登陆失败!    "+"用户名:<input type='text' id='username' name='username' value='' size='15' maxlength='30' />   密码:<input type='password' id='password' name='password' value='' size='15' maxlength='30' />      <input type='button' value='登录' onclick='login();' />";
	}else{
		alert("Unknown return value!");
	}
}

function openLoginPanel(){
	var loginPanel = document.getElementById("loginPanel");
	loginPanel.innerHTML = "用户名:<input type='text' id='username' name='username' value='' size='15' maxlength='30' />   密码:<input type='password' id='password' name='password' value='' size='15' maxlength='30' />      <input type='button' value='登录' onclick='login();' />";
}